﻿using UnityEngine;
using System.Collections;

public class FadePicker : MonoBehaviour, IPicker {

	protected Square sqrCaster;
	protected System.EventHandler<MsgPicker> evtPicker;
	public Collider boxColli;

#region virtual function
	protected virtual bool IsValidTarget(Square sqr) {
		return true;
	}
	
	protected virtual void OnSquareEnter(Square target){
		return;
	}
	
	protected virtual void OnSquareLeave(Square target){
		return;
	}
#endregion

	public void BinderCaster(Square sq)
	{
		sqrCaster = sq;
	}
	
	public void BindEventHandle(System.EventHandler<MsgPicker> msgHandle)
	{
		evtPicker += msgHandle;
	}
	
	public void EnableDetective(bool open)
	{
		boxColli.enabled = open;
	}
	
	public bool IsEnable()
	{
		return boxColli.enabled;
	}

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {

	}

	void OnTriggerEnter(Collider colli) {
		
		// start attack
		if (colli.gameObject.tag == "BodyParty")
		{
			Square target = colli.gameObject.GetComponent<Body>().mainSquare;
			
			if(false == IsValidTarget(target))
				return;
			
			MsgPicker msg = new MsgPicker();
			msg.evt = EPickerEvt.eFindTarget;
			msg.sqrTarget = target;
			
			evtPicker(this, msg);
			Debug.Log("catch sight of enemy:" + target.gameObject.name + " , " + sqrCaster.gameObject.name + " do normal attack");
		}
	}
	
	void OnTriggerExit(Collider colli) {

		// stop attack
		if (colli.gameObject.tag == "BodyParty")
		{
			Square target = colli.gameObject.GetComponent<Body>().mainSquare;
			
			if (false == IsValidTarget(target))
				return;
			
			MsgPicker msg = new MsgPicker();
			msg.evt = EPickerEvt.eNoTarget;
			msg.sqrTarget = target;
			
			evtPicker(this, msg);
			Debug.Log("leave sight of enemy:" + target.gameObject.name + " , " + sqrCaster.gameObject.name + " do normal attack");
		}
	}
}
